package com.aauedu.springboot.controller;

import com.aauedu.springboot.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@Slf4j
public class UploadController {

    //处理转发到用户注册-可以完成文件上传页面
    @GetMapping("/upload.html")
    public String uploadPage() {

        return "upload";  //视图解析，转发到templates/upload.html
    }

    //处理用户的注册请求-包括处理文件上传
    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("name") String name,
                         @RequestParam("email") String email,
                         @RequestParam("age") Integer age,
                         @RequestParam("job") String job,
                         @RequestParam("header") MultipartFile header,
                         @RequestParam("photos") MultipartFile[] photos, HttpSession session) throws IOException, URISyntaxException {
        //输出获取到的信息
        log.info("上传的信息 name={} email={} age={} job={} header={} photos={}",name, email, age, job, header.getSize(), photos.length);

        //如果信息都成功得到，我们就将文件保存放到指定的目录,，比如d:\\截图
        //1、先将文件保存到指定的目录，比如：d:\\截图
        //2、后面我们在演示把文件保存到动态创建的目录
        // D:\技术学习资料\综合技术学习笔记\就业\java\阶段五微服务\springboot\06_springboot-monsters\target\classes\

//        UUID uuid = UUID.randomUUID();
//        String suffix = uuid.toString();
//        log.info("suffix:{}", suffix);

        //获取项目的类路径
        String projectPath = new File("06_springboot-monsters").getAbsolutePath();
        log.info("path={}", projectPath);

        String path = projectPath + "\\target\\classes\\";
        // path=D:\技术学习资料\综合技术学习笔记\就业\java\阶段五微服务\springboot\target\classes
        log.info("path={}", path);

        //动态创建指定的目录
        File file = new File(path + WebUtils.getUploadFileDirectory());

        if (!file.exists()) {//如果目录不存在，则创建，java io
            file.mkdirs();
        }

        if (!header.isEmpty()) {  //处理头像
            //获取上传文件的文件名
           String originalFilename = header.getOriginalFilename();
           log.info("originalFilename:{}", originalFilename);
//            header.transferTo(new File("D:\\截图\\" + originalFilename));
           log.info("保存文件的绝对路径:{}", file.getAbsolutePath());
           //保证上传的文件名不会重复
           String fileName = UUID.randomUUID().toString() + "_" + System.currentTimeMillis() + originalFilename;
            //保存到动态创建的目录
            header.transferTo(new File(file.getAbsolutePath() + "/" + fileName));//动态
        }

        //处理宠物的图片
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {//遍历
                if (!photo.isEmpty()) {
                    String originalFilename = photo.getOriginalFilename();
                   String fileName = UUID.randomUUID().toString() + "_" + System.currentTimeMillis() + originalFilename;

//                    photo.transferTo(new File("D:\\截图\\"  + fileName));
//                    log.info("path={}", file.getAbsolutePath());
                    photo.transferTo(new File(file.getAbsolutePath() + "/" + fileName));//动态

                }
             }
        }

        /*
        //获取上传文件的文件名
        String fileName = header.getOriginalFilename();
        System.out.println("fileName->" + fileName);
        //获取服务器中header目录的路径
        ServletContext servletContext = session.getServletContext();
        String headerPath = servletContext.getRealPath("header");
        System.out.println("servletContext->" + servletContext);
        System.out.println("headerPath->" + headerPath);
        */

        return "文件上传成功!";
    }
}
